{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Demo for the DoWhy causal API\n",
    "We show a simple example of adding a causal extension to any dataframe. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os, sys\n",
    "sys.path.append(os.path.abspath(\"../../../\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import dowhy.datasets\n",
    "import dowhy.api\n",
    "\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "from statsmodels.api import OLS"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>W0</th>\n",
       "      <th>v0</th>\n",
       "      <th>y</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.230046</td>\n",
       "      <td>False</td>\n",
       "      <td>0.328777</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-0.625509</td>\n",
       "      <td>True</td>\n",
       "      <td>3.794958</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-2.600417</td>\n",
       "      <td>False</td>\n",
       "      <td>-5.214096</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.146942</td>\n",
       "      <td>False</td>\n",
       "      <td>1.361876</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-0.919551</td>\n",
       "      <td>False</td>\n",
       "      <td>-2.228864</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>995</th>\n",
       "      <td>-0.063647</td>\n",
       "      <td>False</td>\n",
       "      <td>0.117779</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>996</th>\n",
       "      <td>-1.962547</td>\n",
       "      <td>False</td>\n",
       "      <td>-4.034488</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>997</th>\n",
       "      <td>-0.089728</td>\n",
       "      <td>True</td>\n",
       "      <td>5.780041</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>998</th>\n",
       "      <td>-0.334302</td>\n",
       "      <td>True</td>\n",
       "      <td>3.872871</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>999</th>\n",
       "      <td>-2.410448</td>\n",
       "      <td>False</td>\n",
       "      <td>-4.524930</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1000 rows × 3 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "           W0     v0         y\n",
       "0    0.230046  False  0.328777\n",
       "1   -0.625509   True  3.794958\n",
       "2   -2.600417  False -5.214096\n",
       "3    0.146942  False  1.361876\n",
       "4   -0.919551  False -2.228864\n",
       "..        ...    ...       ...\n",
       "995 -0.063647  False  0.117779\n",
       "996 -1.962547  False -4.034488\n",
       "997 -0.089728   True  5.780041\n",
       "998 -0.334302   True  3.872871\n",
       "999 -2.410448  False -4.524930\n",
       "\n",
       "[1000 rows x 3 columns]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = dowhy.datasets.linear_dataset(beta=5,\n",
    "        num_common_causes=1,\n",
    "        num_instruments = 0,\n",
    "        num_samples=1000,\n",
    "        treatment_is_binary=True)\n",
    "df = data['df']\n",
    "df['y'] = df['y'] + np.random.normal(size=len(df)) # Adding noise to data. Without noise, the variance in Y|X, Z is zero, and mcmc fails.\n",
    "#data['dot_graph'] = 'digraph { v ->y;X0-> v;X0-> y;}'\n",
    "\n",
    "treatment= data[\"treatment_name\"][0]\n",
    "outcome = data[\"outcome_name\"][0]\n",
    "common_cause = data[\"common_causes_names\"][0]\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING:dowhy.causal_model:Causal Graph not provided. DoWhy will construct a graph based on data inputs.\n",
      "INFO:dowhy.causal_graph:If this is observed data (not from a randomized experiment), there might always be missing confounders. Adding a node named \"Unobserved Confounders\" to reflect this.\n",
      "INFO:dowhy.causal_model:Model to find the causal effect of treatment ['v0'] on outcome ['y']\n",
      "INFO:dowhy.causal_identifier:Common causes of treatment and outcome:['W0', 'U']\n",
      "WARNING:dowhy.causal_identifier:If this is observed data (not from a randomized experiment), there might always be missing confounders. Causal effect cannot be identified perfectly.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARN: Do you want to continue by ignoring any unobserved confounders? (use proceed_when_unidentifiable=True to disable this prompt) [y/n] y\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:dowhy.causal_identifier:Instrumental variables for treatment and outcome:[]\n",
      "INFO:dowhy.do_sampler:Using WeightingSampler for do sampling.\n",
      "INFO:dowhy.do_sampler:Caution: do samplers assume iid data.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<matplotlib.axes._subplots.AxesSubplot at 0x7f0f4a609e80>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXIAAAEWCAYAAAB7QRxFAAAAB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Hflnw7Tn8ZKeLQrFl2VphoXX33VV1TpIHuvvBJKmqP0hy69qXvEXE2eqckQMMzh45wOCEHGBwQg4wOCEHGJyQAwxOyAEGJ+QAg/s/zxSquJVs5K4AAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# data['df'] is just a regular pandas.DataFrame\n",
    "df.causal.do(x=treatment,\n",
    "                     variable_types={treatment: 'b', outcome: 'c', common_cause: 'c'},\n",
    "                     outcome=outcome,\n",
    "                     common_causes=[common_cause]).groupby(treatment).mean().plot(y=outcome, kind='bar')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING:dowhy.causal_model:Causal Graph not provided. DoWhy will construct a graph based on data inputs.\n",
      "INFO:dowhy.causal_graph:If this is observed data (not from a randomized experiment), there might always be missing confounders. Adding a node named \"Unobserved Confounders\" to reflect this.\n",
      "INFO:dowhy.causal_model:Model to find the causal effect of treatment ['v0'] on outcome ['y']\n",
      "INFO:dowhy.causal_identifier:Common causes of treatment and outcome:['W0', 'U']\n",
      "WARNING:dowhy.causal_identifier:If this is observed data (not from a randomized experiment), there might always be missing confounders. Causal effect cannot be identified perfectly.\n",
      "INFO:dowhy.causal_identifier:Continuing by ignoring these unobserved confounders because proceed_when_unidentifiable flag is True.\n",
      "INFO:dowhy.causal_identifier:Instrumental variables for treatment and outcome:[]\n",
      "INFO:dowhy.do_sampler:Using WeightingSampler for do sampling.\n",
      "INFO:dowhy.do_sampler:Caution: do samplers assume iid data.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<matplotlib.axes._subplots.AxesSubplot at 0x7f0f48316358>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAA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InfDHwdWAXcVVVPJrkVmKyqncCdwOeT7AeeZSb60unMrUGdduKFtCS1wU+KSlIjDLokNcKgS7MkOWvYM0iLZdAlIMmWJN8Dfti5/ydJ/mXIY0l9MejSjM8Ab6Pz+Ymq+i5wyVAnkvpk0KUZZ1TVT+Y89tJQJpEWaVk/+i+dwg4k2QJU5zeM/h3w30OeSeqL70OXgCSvZWbb5S87D30DuLmq/AVdOm0YdElqhFsuEpDkc8Arrm6qaus8y6VTkkGXZnxj1u01wNX8/+8BkE55brlI80hyBvBwVf3ZsGeReuXbFqX5nQ/8/rCHkPrhlosEJHmOl/fQz2Dm10DfMryJpP655aIVr/OF5ht4+Zu4jvkFLTodueWiFa8T711V9VLnx5jrtGTQpRl7k2we9hDSUrjlohUtyerO1yw+Cfwh8CPgf4Ewc/F+0VAHlPrgi6Ja6f4TuAh4+7AHkZbKoGulC0BV/WjYg0hLZdC10o0l+YeFDlbVJ5dzGGkpDLpWulXAq+hcqUunM18U1YqWZI8vfKoVvm1RK51X5mqGV+ha0ZK8pqqeHfYc0iAYdElqhFsuktQIgy5JjTDo0jyS3JDkh52fG4Y9j9QL99ClOZK8BpgEJpj5HemPAW+oqueGOpjUhR8s0oqW5OPAgaq6vXP/H4GXgIeOv/slyUPA5cC9w5pT6oVbLlrp7gf+dtb947dnf0H0FLBu2SaSFskrdK1oVfV4ktcm+QNgDHgO+DWwZriTSf3zCl2CB4BrgHcwc8V+kJmvpDtuPS9/PZ10yvJFUa14SV4PfA5YC/w58BtmXgg9/jte9jDzoqifKNUpzS0XrXhV9WSSs4GDVfUMQJJ/AnZ3ltxqzHU68ApdkhrhHrokNcKgS1IjDLokNcKgS1IjDLokNcKgS1IjDLokNcKgS1Ij/g/hAqArde3ybAAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "df.causal.do(x={treatment: 1}, \n",
    "              variable_types={treatment:'b', outcome: 'c', common_cause: 'c'}, \n",
    "              outcome=outcome,\n",
    "              method='weighting', \n",
    "              common_causes=[common_cause],\n",
    "              proceed_when_unidentifiable=True).groupby(treatment).mean().plot(y=outcome, kind='bar')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:dowhy.causal_model:Model to find the causal effect of treatment ['v0'] on outcome ['y']\n",
      "INFO:dowhy.causal_identifier:Common causes of treatment and outcome:['W0', 'U']\n",
      "WARNING:dowhy.causal_identifier:If this is observed data (not from a randomized experiment), there might always be missing confounders. Causal effect cannot be identified perfectly.\n",
      "INFO:dowhy.causal_identifier:Continuing by ignoring these unobserved confounders because proceed_when_unidentifiable flag is True.\n",
      "INFO:dowhy.causal_identifier:Instrumental variables for treatment and outcome:[]\n",
      "INFO:dowhy.do_sampler:Using WeightingSampler for do sampling.\n",
      "INFO:dowhy.do_sampler:Caution: do samplers assume iid data.\n",
      "INFO:dowhy.causal_model:Model to find the causal effect of treatment ['v0'] on outcome ['y']\n",
      "INFO:dowhy.causal_identifier:Common causes of treatment and outcome:['W0', 'U']\n",
      "WARNING:dowhy.causal_identifier:If this is observed data (not from a randomized experiment), there might always be missing confounders. Causal effect cannot be identified perfectly.\n",
      "INFO:dowhy.causal_identifier:Continuing by ignoring these unobserved confounders because proceed_when_unidentifiable flag is True.\n",
      "INFO:dowhy.causal_identifier:Instrumental variables for treatment and outcome:[]\n",
      "INFO:dowhy.do_sampler:Using WeightingSampler for do sampling.\n",
      "INFO:dowhy.do_sampler:Caution: do samplers assume iid data.\n"
     ]
    }
   ],
   "source": [
    "cdf_1 = df.causal.do(x={treatment: 1}, \n",
    "              variable_types={treatment: 'b', outcome: 'c', common_cause: 'c'}, \n",
    "              outcome=outcome, \n",
    "              dot_graph=data['dot_graph'],\n",
    "              proceed_when_unidentifiable=True)\n",
    "\n",
    "cdf_0 = df.causal.do(x={treatment: 0}, \n",
    "              variable_types={treatment: 'b', outcome: 'c', common_cause: 'c'}, \n",
    "              outcome=outcome, \n",
    "              dot_graph=data['dot_graph'],\n",
    "              proceed_when_unidentifiable=True)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>W0</th>\n",
       "      <th>v0</th>\n",
       "      <th>y</th>\n",
       "      <th>propensity_score</th>\n",
       "      <th>weight</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-0.583286</td>\n",
       "      <td>False</td>\n",
       "      <td>-2.456860</td>\n",
       "      <td>0.574597</td>\n",
       "      <td>1.740350</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-1.853274</td>\n",
       "      <td>False</td>\n",
       "      <td>-3.699267</td>\n",
       "      <td>0.754520</td>\n",
       "      <td>1.325346</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-1.530711</td>\n",
       "      <td>False</td>\n",
       "      <td>-4.384006</td>\n",
       "      <td>0.713824</td>\n",
       "      <td>1.400906</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-0.036469</td>\n",
       "      <td>False</td>\n",
       "      <td>-2.269524</td>\n",
       "      <td>0.486654</td>\n",
       "      <td>2.054847</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-1.749115</td>\n",
       "      <td>False</td>\n",
       "      <td>-3.311137</td>\n",
       "      <td>0.741816</td>\n",
       "      <td>1.348043</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>995</th>\n",
       "      <td>-1.227516</td>\n",
       "      <td>False</td>\n",
       "      <td>-0.875867</td>\n",
       "      <td>0.672107</td>\n",
       "      <td>1.487859</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>996</th>\n",
       "      <td>-0.405553</td>\n",
       "      <td>False</td>\n",
       "      <td>-1.427872</td>\n",
       "      <td>0.546258</td>\n",
       "      <td>1.830637</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>997</th>\n",
       "      <td>0.230046</td>\n",
       "      <td>False</td>\n",
       "      <td>0.328777</td>\n",
       "      <td>0.443752</td>\n",
       "      <td>2.253509</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>998</th>\n",
       "      <td>-2.157752</td>\n",
       "      <td>False</td>\n",
       "      <td>-4.830362</td>\n",
       "      <td>0.789181</td>\n",
       "      <td>1.267136</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>999</th>\n",
       "      <td>-0.453149</td>\n",
       "      <td>False</td>\n",
       "      <td>-2.167928</td>\n",
       "      <td>0.553884</td>\n",
       "      <td>1.805431</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1000 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "           W0     v0         y  propensity_score    weight\n",
       "0   -0.583286  False -2.456860          0.574597  1.740350\n",
       "1   -1.853274  False -3.699267          0.754520  1.325346\n",
       "2   -1.530711  False -4.384006          0.713824  1.400906\n",
       "3   -0.036469  False -2.269524          0.486654  2.054847\n",
       "4   -1.749115  False -3.311137          0.741816  1.348043\n",
       "..        ...    ...       ...               ...       ...\n",
       "995 -1.227516  False -0.875867          0.672107  1.487859\n",
       "996 -0.405553  False -1.427872          0.546258  1.830637\n",
       "997  0.230046  False  0.328777          0.443752  2.253509\n",
       "998 -2.157752  False -4.830362          0.789181  1.267136\n",
       "999 -0.453149  False -2.167928          0.553884  1.805431\n",
       "\n",
       "[1000 rows x 5 columns]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cdf_0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>W0</th>\n",
       "      <th>v0</th>\n",
       "      <th>y</th>\n",
       "      <th>propensity_score</th>\n",
       "      <th>weight</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-0.229733</td>\n",
       "      <td>True</td>\n",
       "      <td>4.754793</td>\n",
       "      <td>0.482075</td>\n",
       "      <td>2.074366</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-0.238390</td>\n",
       "      <td>True</td>\n",
       "      <td>4.487643</td>\n",
       "      <td>0.480676</td>\n",
       "      <td>2.080404</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-2.161507</td>\n",
       "      <td>True</td>\n",
       "      <td>-1.700668</td>\n",
       "      <td>0.210415</td>\n",
       "      <td>4.752520</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-1.639879</td>\n",
       "      <td>True</td>\n",
       "      <td>2.173829</td>\n",
       "      <td>0.271959</td>\n",
       "      <td>3.677028</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.030840</td>\n",
       "      <td>True</td>\n",
       "      <td>4.465655</td>\n",
       "      <td>0.524224</td>\n",
       "      <td>1.907580</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>995</th>\n",
       "      <td>-1.688769</td>\n",
       "      <td>True</td>\n",
       "      <td>2.404017</td>\n",
       "      <td>0.265737</td>\n",
       "      <td>3.763121</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>996</th>\n",
       "      <td>-0.910527</td>\n",
       "      <td>True</td>\n",
       "      <td>2.528176</td>\n",
       "      <td>0.374608</td>\n",
       "      <td>2.669460</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>997</th>\n",
       "      <td>-2.838431</td>\n",
       "      <td>True</td>\n",
       "      <td>-0.226373</td>\n",
       "      <td>0.146704</td>\n",
       "      <td>6.816457</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>998</th>\n",
       "      <td>0.429132</td>\n",
       "      <td>True</td>\n",
       "      <td>6.865883</td>\n",
       "      <td>0.587791</td>\n",
       "      <td>1.701285</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>999</th>\n",
       "      <td>-1.509348</td>\n",
       "      <td>True</td>\n",
       "      <td>2.153207</td>\n",
       "      <td>0.289010</td>\n",
       "      <td>3.460088</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1000 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "           W0    v0         y  propensity_score    weight\n",
       "0   -0.229733  True  4.754793          0.482075  2.074366\n",
       "1   -0.238390  True  4.487643          0.480676  2.080404\n",
       "2   -2.161507  True -1.700668          0.210415  4.752520\n",
       "3   -1.639879  True  2.173829          0.271959  3.677028\n",
       "4    0.030840  True  4.465655          0.524224  1.907580\n",
       "..        ...   ...       ...               ...       ...\n",
       "995 -1.688769  True  2.404017          0.265737  3.763121\n",
       "996 -0.910527  True  2.528176          0.374608  2.669460\n",
       "997 -2.838431  True -0.226373          0.146704  6.816457\n",
       "998  0.429132  True  6.865883          0.587791  1.701285\n",
       "999 -1.509348  True  2.153207          0.289010  3.460088\n",
       "\n",
       "[1000 rows x 5 columns]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cdf_1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Comparing the estimate to Linear Regression\n",
    "First, estimating the effect using the causal data frame, and the 95% confidence interval."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAANMAAAASCAYAAADBs+vIAAAABHNCSVQICAgIfAhkiAAABkxJREFUaIHt2nnsXVURB/CP0FKs4l5t3FKtC1WJiJHiAr5CbFQCohhjVHAJbjGmGgMuMfGniVGREAlVKUFFJekfElo2FcQNRMVoNG5BK/RXaBTZxBZLi5bnH3Ou73K59757z+/1acz7Ji/3l5k5Z87MnGXOnB8zzDDDPsE8hg2/WzL6eyK+jD9jT+r/c3jkhOQLHINNaYx7Uvsr8Moa2dfibFyDHcK2C1r6fhDejutwN/6Bn+Nd2G/MuAq8yciPpzTIzOvn+7e0yBe/vRO0pY+P4Vhcie24BzfiG3jhhGzpq4N9HMtFNcJ/FxO4irs7KiuwEj/GY3ExrsfhWIeX48W4YwHyBU7HqcKhl+B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TknyGxr4xe5xTKY8vC3Rqu8PBYoF+qGWcQ6MX0xNOFA8XpbvC48w/vGx+NW9J9Whq+/PSnJzHfsto84W8nxcnKTVJ44CFyX+iS3jGWfLJHSUMZFY1hUg6nBE+nZ9H/h++q4Vpe5yhe4g8QC7Cz/NlCcKEUM8q6YNo8vy1o5j7ovXi8rPxhJtj6g81eEwPA8/Ejte12pjF98fiJPEbtykvw11tpDn42IXX9agq6BXK2YFutiyUB1VTDyWq9z/HafACnGpG3pgNWOlOCLrHken8WhbXBzfX6GvFYH/m9jZmzAw/mR6WA3tULGL3YnHt7QtY05zapDj+zJOSjKXjhlDji19ffw6o3vJEyptXpHa3INHN4yxiy25OqYRy/8I7MTl+ILIly9MgxomevXhbj7xVtT0txJ/TfzN+JR4FBuK1Vw1tK888b98NyWZq0TacaG4NP5T/TF/gnjNP9/oQnpDiXZGRf46UbFan8a0OfW9Ay+t6b8Jc5oDMKe/78u4RvtlvUCOLX19vJ8oTQ9Tv19N9lwiJvm4e2wXW3J1TCOWpM42ihr7XUnJbWnQJ4t/p6hiXvNiIv6X6Sv4izhyt2n/x9W+8sSRfnaSvVe8J2wSD3p1mNOe/85X5E/FL4RP9oh06/NikvVBobcuADm+L7DK+MJDgVxb+vp4Md4n0vIdYuHdKsrcaydkS46OacRyhhlmmGGGGWaYYYb/L/wbSslG770ekzEAAAAASUVORK5CYII=\n",
      "text/latex": [
       "$\\displaystyle 5.006194579687894$"
      ],
      "text/plain": [
       "5.006194579687894"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(cdf_1['y'] - cdf_0['y']).mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAN8AAAASCAYAAADbjwtGAAAABHNCSVQICAgIfAhkiAAABbtJREFUaIHt2mvIZVUZB/Df6DtppmlWKpm39IMvCJkfHE3UposhmqSRS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hYxSmxT/HL7nxPQ2bMZr7TNPOS1LbJy3n65PIL4hU8ymRX5cxkPfyHZbaVbeaV+IykWZtwb9Enn84nkhtDplQx/tS+W01bXYT50vbhPObMM5mfegYh2ny7+qXnPjOGVcTmrgjgnVJzCJ1KN70D7RQVMaa1O5x7dYUbeT3Up9X1/3qzlvqsGeS39JSfldxSPuPlvKjdBSzYnULvMBtqb7puKWNzfrQMQ6z4l9Gk19y4nua49qBe3m38970PElsxZZ3hPYQB5Cv4A8tFBW4TOxEPioOQDeOFm8tvxXfa6g7Cu8RtzDW4fctx3pMetbtoNXhLLGb9uOW8qN0FF/vtze0K8qrO6q0t1kfOsZhFvyraPJLTnxPc1xj46vrIeSh4jO/sqavL6Y2D2u3Nugq34SB5rRzUf055cFiAb0kzq/KeHON/JFiZn0R75iCjjMtr1/2r9SdLAJls7juVUYXm/Whg/74d/UL3eO767g6cR93vexpsWhdjWfxXtvnrEMcJHLrYan8PPxQ5MPXiPuAVQyTTI78KAzEjlPd9bKByPnvF4vyTYLzKSJd+aW4PVGeyR4SBn4iyS8m+c3i9sd9U9CxkwiMDyb5tcLhizhV+GmN2FQo0NVmfejok39Xv9A9vruOK4f7djgAPxD3+V5NnTRdPB2Kt/ngSvnA+LXYbyeQH4Wir7ov34kiHXlGLNBfEzPlb8QdvepkBJ/HI0l+q0gbrhUXdOuQo4PIHtaItOclscO2AT8XqVITzy4260NHX/y7+qVAl/juOq5c7nPMMcccc8wxxxxzzDEr/A9hbqkj3YekPAAAAABJRU5ErkJggg==\n",
      "text/latex": [
       "$\\displaystyle 0.2245958282589085$"
      ],
      "text/plain": [
       "0.2245958282589085"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "1.96*(cdf_1['y'] - cdf_0['y']).std() / np.sqrt(len(df))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Comparing to the estimate from OLS."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"simpletable\">\n",
       "<caption>OLS Regression Results</caption>\n",
       "<tr>\n",
       "  <th>Dep. Variable:</th>            <td>y</td>        <th>  R-squared (uncentered):</th>      <td>   0.938</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Model:</th>                   <td>OLS</td>       <th>  Adj. R-squared (uncentered):</th> <td>   0.938</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Method:</th>             <td>Least Squares</td>  <th>  F-statistic:       </th>          <td>   7544.</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Date:</th>             <td>Tue, 07 Jan 2020</td> <th>  Prob (F-statistic):</th>           <td>  0.00</td> \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Time:</th>                 <td>11:54:35</td>     <th>  Log-Likelihood:    </th>          <td> -1408.7</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>No. Observations:</th>      <td>  1000</td>      <th>  AIC:               </th>          <td>   2821.</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Df Residuals:</th>          <td>   998</td>      <th>  BIC:               </th>          <td>   2831.</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Df Model:</th>              <td>     2</td>      <th>                     </th>              <td> </td>   \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Covariance Type:</th>      <td>nonrobust</td>    <th>                     </th>              <td> </td>   \n",
       "</tr>\n",
       "</table>\n",
       "<table class=\"simpletable\">\n",
       "<tr>\n",
       "   <td></td>     <th>coef</th>     <th>std err</th>      <th>t</th>      <th>P>|t|</th>  <th>[0.025</th>    <th>0.975]</th>  \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>x1</th> <td>    2.2679</td> <td>    0.025</td> <td>   89.049</td> <td> 0.000</td> <td>    2.218</td> <td>    2.318</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>x2</th> <td>    5.0091</td> <td>    0.050</td> <td>  100.297</td> <td> 0.000</td> <td>    4.911</td> <td>    5.107</td>\n",
       "</tr>\n",
       "</table>\n",
       "<table class=\"simpletable\">\n",
       "<tr>\n",
       "  <th>Omnibus:</th>       <td> 2.664</td> <th>  Durbin-Watson:     </th> <td>   2.034</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Prob(Omnibus):</th> <td> 0.264</td> <th>  Jarque-Bera (JB):  </th> <td>   2.431</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Skew:</th>          <td>-0.049</td> <th>  Prob(JB):          </th> <td>   0.297</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Kurtosis:</th>      <td> 2.779</td> <th>  Cond. No.          </th> <td>    2.03</td>\n",
       "</tr>\n",
       "</table><br/><br/>Warnings:<br/>[1] Standard Errors assume that the covariance matrix of the errors is correctly specified."
      ],
      "text/plain": [
       "<class 'statsmodels.iolib.summary.Summary'>\n",
       "\"\"\"\n",
       "                                 OLS Regression Results                                \n",
       "=======================================================================================\n",
       "Dep. Variable:                      y   R-squared (uncentered):                   0.938\n",
       "Model:                            OLS   Adj. R-squared (uncentered):              0.938\n",
       "Method:                 Least Squares   F-statistic:                              7544.\n",
       "Date:                Tue, 07 Jan 2020   Prob (F-statistic):                        0.00\n",
       "Time:                        11:54:35   Log-Likelihood:                         -1408.7\n",
       "No. Observations:                1000   AIC:                                      2821.\n",
       "Df Residuals:                     998   BIC:                                      2831.\n",
       "Df Model:                           2                                                  \n",
       "Covariance Type:            nonrobust                                                  \n",
       "==============================================================================\n",
       "                 coef    std err          t      P>|t|      [0.025      0.975]\n",
       "------------------------------------------------------------------------------\n",
       "x1             2.2679      0.025     89.049      0.000       2.218       2.318\n",
       "x2             5.0091      0.050    100.297      0.000       4.911       5.107\n",
       "==============================================================================\n",
       "Omnibus:                        2.664   Durbin-Watson:                   2.034\n",
       "Prob(Omnibus):                  0.264   Jarque-Bera (JB):                2.431\n",
       "Skew:                          -0.049   Prob(JB):                        0.297\n",
       "Kurtosis:                       2.779   Cond. No.                         2.03\n",
       "==============================================================================\n",
       "\n",
       "Warnings:\n",
       "[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.\n",
       "\"\"\""
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model = OLS(np.asarray(df[outcome]), np.asarray(df[[common_cause, treatment]], dtype=np.float64))\n",
    "result = model.fit()\n",
    "result.summary()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
